package com.zyy.game.tianlong.handler;

import com.zyy.game.tianlong.codes.AccountCode;
import com.zyy.game.tianlong.codes.OpCode;
import com.zyy.game.tianlong.dto.account.AccountDto;
import com.zyy.game.tianlong.dto.account.ChangeInfoDto;
import com.zyy.game.tianlong.entity.GamePlayer;
import com.zyy.game.tianlong.network.GameContext;
import com.zyy.game.tianlong.network.GameManage;
import com.zyy.game.tianlong.network.annotation.GameEvent;
import com.zyy.game.tianlong.network.annotation.GameModule;
import com.zyy.game.tianlong.network.annotation.GameResult;
import com.zyy.game.tianlong.serviceimpl.AccountServiceImpl;
import com.zyy.game.tianlong.serviceimpl.TaskServiceImpl;
import com.zyy.game.tianlong.tool.WebUtils;
import com.zyy.game.tianlong.vo.ResultVo;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * 玩家信息操作
 *
 * @author relufi
 * @date 2019/8/15 0015
 */
@Component
@GameModule(OpCode.ACCOUNT)
public class PlayerGameHandler {
    private final AccountServiceImpl accountService;
    private final TaskServiceImpl taskService;
    private final GameManage gameManage;

    public PlayerGameHandler(AccountServiceImpl accountService, TaskServiceImpl taskService, GameManage gameManage) {
        this.accountService = accountService;
        this.taskService = taskService;
        this.gameManage = gameManage;
    }

    /**
     * 获取重置密码手机短信验证码
     *
     * @param phone 手机号
     */
    @GameEvent(AccountCode.RESET_PASSWORD_PHONE_CODE)
    @GameResult(defaultSuccess = true)
    public void resetPasswordPhoneCode(String phone) {
        accountService.phoneCodeResetPw(phone);
    }

    /**
     * 重置密码
     *
     * @param accountDto 传输数据
     * @param ctx        game
     */
    @GameEvent(AccountCode.RESET_PASSWORD)
    public void resetPassWord(AccountDto accountDto, GameContext ctx) {
        accountService.resetPassWord(accountDto.getPhone(), accountDto.getAuthCode(), accountDto.getPassWord());
        ctx.writeAndFlush(OpCode.ACCOUNT, AccountCode.RESET_PASSWORD, ResultVo.success());
        gameManage.remove(ctx);
    }

    /**
     * 获取增加交易密码手机验证码
     *
     * @param ctx game
     */
    @GameEvent(AccountCode.ADD_PAY_PASSWORD_PHONE_CODE)
    @GameResult(defaultSuccess = true)
    public void addPayPassWordPhoneCode(GameContext ctx) {
        accountService.phoneCodePayPw(GameManage.getGamePlayer(ctx).getPhone());
    }

    /**
     * 首次打开银行，以手机验证码的方式增加交易密码
     *
     * @param accountDto 传输数据
     * @param ctx        game
     */
    @GameEvent(AccountCode.ADD_PAY_PASSWORD)
    @GameResult(defaultSuccess = true)
    public void addPayPassWord(AccountDto accountDto, GameContext ctx) {
        accountService.addPayPassWord(GameManage.getGamePlayer(ctx), accountDto.getPassWord(), accountDto.getAuthCode());
    }

    /**
     * 首次打开银行，微信登录 直接增加交易密码
     *
     * @param payPassWord 支付密码
     */
    @GameEvent(AccountCode.ADD_WX_PAY_PASSWORD)
    @GameResult(defaultSuccess = true)
    public void addWxPayPassWord(String payPassWord, GameContext ctx) {
        GamePlayer gamePlayer = GameManage.getGamePlayer(ctx);
        if (gamePlayer.getPhone() == null) {
            accountService.addWxPayPassWord(gamePlayer, payPassWord);
        }
    }

    /**
     * 签到 连续签到领取奖励
     *
     * @param gameContext game
     */
    @GameEvent(AccountCode.SIGN_IN)
    @GameResult(defaultSuccess = true)
    public void signIn(GameContext gameContext) {
        accountService.signInReward(GameManage.getGamePlayer(gameContext));
    }

    /**
     * 修改个人信息 （名称 签名 头像）
     *
     * @param changeInfoDto 传输的数据
     * @param gameContext   game
     */
    @GameEvent(AccountCode.CHANGE_INFO)
    @GameResult
    public void changeInfo(ChangeInfoDto changeInfoDto, GameContext gameContext) {
        GamePlayer gamePlayer = GameManage.getGamePlayer(gameContext);
        String photo = changeInfoDto.getPhoto();
        if (photo != null) {
            String idStr = gamePlayer.getPlayerId().toString();
            int length = idStr.length();
            String pathStr = "/resource/static/" + idStr.charAt(length - 1) + "/" + ((length == 1) ? '0' : idStr.charAt(length - 2)) + "/" + idStr + ".png";
            File path = new File(WebUtils.CLASS_PATH, pathStr);
            taskService.saveFile(path, photo);
            changeInfoDto.setPhoto(pathStr);
        }
        accountService.changeInfo(changeInfoDto, gamePlayer);
        gameContext.writeAndFlush(ResultVo.success(changeInfoDto));
    }
}
